package com.seaboxdata.interfaceJob;

import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.TransportationVehicle;
import com.seaboxdata.entity.TransportationVehicleVo;
import com.seaboxdata.service.DpjfxtTokenService;
import com.seaboxdata.service.TransportationVehicleService;
import com.seaboxdata.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @create 2021-04-27 16:20
 **/
@Component
@Configuration
@Slf4j
public class DpjfxtTransportationVehicleJob {

    @Autowired
    private DpjfxtTokenService dpjfxtTokenService;

    @Autowired
    private TransportationVehicleService transportationVehicleService;

    @Value("${dpjfxt.transportation.vehicle.url}")
    private String url;

    @Value("${dpjfxt.transportation.vehicle.pageSize}")
    private String pageSize;

    @Value("${interface.isTest}")
    private String isTest;

    @Scheduled(cron = "${dpjfxt.transportation.vehicle.interval}")//每隔5分钟执行一次
    public void doCollect() {
        log.info("get transportation vehicle begin");
        //如果是测试,直接退出
        if ("true".equals(isTest)) {
            return;
        }
        String token = dpjfxtTokenService.getToken();
        String format = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String startTime = sdf.format(DateUtils.addDays(new Date(), -1)) + " 00:00:00";
        String endTime = sdf.format(new Date()) + " 00:00:00";

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("content-type", "application/json");
        if (StringUtils.isEmpty(token)) {
            log.error("get token error");
            return;
        }
        headerMap.put("token", token);
        handler(headerMap, Integer.valueOf(pageSize), 1,startTime,endTime);
    }

    public void handler(Map<String, String> headerMap, int pageSize, int pageNum,String startTime,String endTime) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageNum", pageNum);
        paramMap.put("pageSize", pageSize);
        paramMap.put("startTime",startTime);
        paramMap.put("endTime",endTime);
        HttpUtils.Response response = HttpUtils.doPost(url, JSONObject.toJSONString(paramMap), "utf-8", headerMap);
        if (response.getStatusCode() == 200) {
            String result = response.getResponseData();
            TransportationVehicleVo transportationVehicleVo = JSONObject.parseObject(result, TransportationVehicleVo.class);
            if (transportationVehicleVo.getStatus() != 200) {
                log.error(transportationVehicleVo.getMessage());
            } else {
                List<TransportationVehicle> transportationVehicles = transportationVehicleVo.getData().getRecords();
                if (!CollectionUtils.isEmpty(transportationVehicles)) {
                    transportationVehicles.forEach(x -> x.setCjsj(new Date()));
                    transportationVehicleService.saveBatch(transportationVehicles);
                }

                if (pageNum < transportationVehicleVo.getData().getPages()) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler(headerMap, pageSize, pageNum + 1,startTime,endTime);
                }
            }
        }
    }
}
